package com.liao.app2.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.liao.app2.R;
import com.liao.app2.ui.bean.User;
import com.liao.app2.ui.db.UserDB;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText username;
    private EditText password;
    private EditText et_sex;
    private Button btn_login;
    private Button btn_register;

    private boolean isFirstRun;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_examine2);
        initView();
        check();
    }

    private void check() {
        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        isFirstRun = sp.getBoolean("run", true);
        if (isFirstRun) {
            et_sex.setVisibility(View.GONE);
            sp.edit().putBoolean("run", false).apply();
        } else {
            et_sex.setVisibility(View.VISIBLE);
            boolean isUpdate = sp.getBoolean("isUpdate", false);
            if (!isUpdate) {
                userDB.onUpgrade(userDB.getWritableDatabase(), 1, 2);
                sp.edit().putBoolean("isUpdate", true).apply();
            }
        }
    }

    UserDB userDB;

    private void initView() {
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        et_sex = (EditText) findViewById(R.id.et_sex);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_register = (Button) findViewById(R.id.btn_register);

        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        userDB = new UserDB(this, "users", 1);
    }

    boolean isLogin = false;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                isLogin = true;
                submit();
                break;
            case R.id.btn_register:
                isLogin = false;
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String usernameString = username.getText().toString().trim();
        if (TextUtils.isEmpty(usernameString)) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String passwordString = password.getText().toString().trim();
        if (TextUtils.isEmpty(passwordString)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String sexString = et_sex.getText().toString().trim();
        if (TextUtils.isEmpty(sexString) && !isFirstRun) {
            Toast.makeText(this, "性别不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (isLogin) {
            login(usernameString, passwordString, sexString);
        } else {
            register(usernameString, passwordString, sexString);
        }
    }

    private void login(String name, String pwd, String sex) {
        boolean user = userDB.findUser(userDB.getReadableDatabase(), name, pwd);
        if (user) {
            startActivity(new Intent(this, Main2Activity.class));
        } else {
            Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void register(String name, String pwd, String sex) {
        User user = new User();
        user.setName(name);
        user.setPw(pwd);
        if (!isFirstRun) {
            user.setSex(sex);
        }
        boolean b = userDB.saveData(userDB.getWritableDatabase(), user);
        if (b) {
            Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();
        }
    }
}
